(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("paySwiftService", paySwiftService);
	paySwiftService.$inject = ['loggerFactory'];
	function paySwiftService(loggerFactory) {
		var log = loggerFactory.getLogger("paySwiftService");
		var SCMToSWIFTMapping = {
			//naming pattern: numOfParty-receiverOfMT103
		    "4-BI": {
		        "OC": {scmPartyIndex:"R2", MT103Tag: [":50: (Ordering Customer)"], MT202COVTag: []},
		        "OI": {scmPartyIndex:"R3", MT103Tag: [":52: (Ordering Institution)"], MT202COVTag: []},
		        "BC": {scmPartyIndex:"R4", MT103Tag: [":59: (Beneficiary Customer)"], MT202COVTag: []},
		        "BI": {scmPartyIndex:"R5", MT103Tag: ["{2 Receiver BIC}"], MT202COVTag: []}
		    },
		    "5-BI": {
		        "OC": {scmPartyIndex:"R2", MT103Tag: [":50: (Ordering Customer)"], MT202COVTag: [":50: (Ordering Customer)"]},
		        "OI": {scmPartyIndex:"R3", MT103Tag: [":52: (Ordering Institution)"], MT202COVTag: [":52: (Ordering Institution)"]},
		        "BC": {scmPartyIndex:"R4", MT103Tag: [":59: (Beneficiary Customer)"], MT202COVTag: [":59: (Beneficiary Customer)"]},
		        "BI": {scmPartyIndex:"R5", MT103Tag: ["{2 Receiver BIC}"], MT202COVTag: [":58: (Beneficiary Institution)"]},
		        "CI": {scmPartyIndex:"R6", MT103Tag: [":53: (Sender\'s Correspondent)"], MT202COVTag: ["{2 Receiver BIC}"]}
		    },
		    "5-CI": {
		        "OC": {scmPartyIndex:"R2", MT103Tag: [":50: (Ordering Customer)"], MT202COVTag: []},
		        "OI": {scmPartyIndex:"R3", MT103Tag: [":52: (Ordering Institution)"], MT202COVTag: []},
		        "BC": {scmPartyIndex:"R4", MT103Tag: [":59: (Beneficiary Customer)"], MT202COVTag: []},
		        "BI": {scmPartyIndex:"R5", MT103Tag: [":57: (Account With Institution)"], MT202COVTag: []},
		        "CI": {scmPartyIndex:"R6", MT103Tag: ["{2 Receiver BIC}"], MT202COVTag: []}
		    },
		    "6-BI": {
		        "OC": {scmPartyIndex:"R2", MT103Tag: [":50: (Ordering Customer)"], MT202COVTag: [":50: (Ordering Customer)"]},
		        "OI": {scmPartyIndex:"R3", MT103Tag: [":52: (Ordering Institution)"], MT202COVTag: [":52: (Ordering Institution)"]},
		        "BC": {scmPartyIndex:"R4", MT103Tag: [":59: (Beneficiary Customer)"], MT202COVTag: [":59: (Beneficiary Customer)"]},
		        "BI": {scmPartyIndex:"R5", MT103Tag: ["{2 Receiver BIC}"], MT202COVTag: [":58: (Beneficiary Institution)"]},
		        "CI": {scmPartyIndex:"R6", MT103Tag: [":53: (Sender\' Correspondent)"], MT202COVTag: ["{2 Receiver BIC}"]},
		        "II": {scmPartyIndex:"R7", MT103Tag: [":54: (Receiver\' Correspondent)"], MT202COVTag: [":57: (Account With Institution)"]}
		    },
		    "6-II": {
		        "OC": {scmPartyIndex:"R2", MT103Tag: [":50: (Ordering Customer)"], MT202COVTag: [":50: (Ordering Customer)"]},
		        "OI": {scmPartyIndex:"R3", MT103Tag: [":52: (Ordering Institution)"], MT202COVTag: [":52: (Ordering Institution)"]},
		        "BC": {scmPartyIndex:"R4", MT103Tag: [":59: (Beneficiary Customer)"], MT202COVTag: [":59: (Beneficiary Customer)"]},
		        "BI": {scmPartyIndex:"R5", MT103Tag: [":57: (Account With Institution)"], MT202COVTag: [":58: (Beneficiary Institution)"]},
		        "CI": {scmPartyIndex:"R6", MT103Tag: [":53: (Sender\'s Correspondent)"], MT202COVTag: ["{2 Receiver BIC}"]},
		        "II": {scmPartyIndex:"R7", MT103Tag: ["{2 Receiver BIC}"], MT202COVTag: [":57: (Account With Institution)"]}
		    },
		    "6-CI": {
		        "OC": {scmPartyIndex:"R2", MT103Tag: [":50: (Ordering Customer)"], MT202COVTag: []},
		        "OI": {scmPartyIndex:"R3", MT103Tag: [":52: (Ordering Institution)"], MT202COVTag: []},
		        "BC": {scmPartyIndex:"R4", MT103Tag: [":59: (Beneficiary Customer)"], MT202COVTag: []},
		        "BI": {scmPartyIndex:"R5", MT103Tag: [":57: (Account With Institution)"], MT202COVTag: []},
		        "CI": {scmPartyIndex:"R6", MT103Tag: ["{2 Receiver BIC}"], MT202COVTag: []},
		        "II": {scmPartyIndex:"R7", MT103Tag: [":56: (Intermediary)"], MT202COVTag: []}
		    },
		    "7-BI": {
		        "OC": {scmPartyIndex:"R2", MT103Tag: [":50: (Ordering Customer)"], MT202COVTag: [":50: (Ordering Customer)"]},
		        "OI": {scmPartyIndex:"R3", MT103Tag: [":52: (Ordering Institution)"], MT202COVTag: [":52: (Ordering Institution)"]},
		        "BC": {scmPartyIndex:"R4", MT103Tag: [":59: (Beneficiary Customer)"], MT202COVTag: [":59: (Beneficiary Customer)"]},
		        "BI": {scmPartyIndex:"R5", MT103Tag: [":57: (Account With Institution)", "{2 Receiver BIC}"], MT202COVTag: [":58: (Beneficiary Institution)"]},
		        "CI": {scmPartyIndex:"R6", MT103Tag: [":53: (Sender\'s Correspondent)"], MT202COVTag: ["{2 Receiver BIC}"]},
		        "II": {scmPartyIndex:"R7", MT103Tag: [":54: (Receiver\'s Correspondent)"], MT202COVTag: [":56: (Intermediary)"]},
		        "AI": {scmPartyIndex:"R8", MT103Tag: [":56: (Intermediary)"], MT202COVTag: [":57: (Account With Institution)"]}
		    },
		    "7-AI": {
		        "OC": {scmPartyIndex:"R2", MT103Tag: [":50: (Ordering Customer)"], MT202COVTag: [":50: (Ordering Customer)"]},
		        "OI": {scmPartyIndex:"R3", MT103Tag: [":52: (Ordering Institution)"], MT202COVTag: [":52: (Ordering Institution)"]},
		        "BC": {scmPartyIndex:"R4", MT103Tag: [":59: (Beneficiary Customer)"], MT202COVTag: [":59: (Beneficiary Customer)"]},
		        "BI": {scmPartyIndex:"R5", MT103Tag: [":57: (Account With Institution)"], MT202COVTag: [":58: (Beneficiary Institution)"]},
		        "CI": {scmPartyIndex:"R6", MT103Tag: [":53: (Sender\'s Correspondent)"], MT202COVTag: ["{2 Receiver BIC}"]},
		        "II": {scmPartyIndex:"R7", MT103Tag: [":54: (Receiver\'s Correspondent)"], MT202COVTag: [":56: (Intermediary)"]},
		        "AI": {scmPartyIndex:"R8", MT103Tag: ["{2 Receiver BIC}"], MT202COVTag: [":57: (Account With Institution)"]}
		    },
		    "7-II": {
		        "OC": {scmPartyIndex:"R2", MT103Tag: [":50: (Ordering Customer)"], MT202COVTag: [":50: (Ordering Customer)"]},
		        "OI": {scmPartyIndex:"R3", MT103Tag: [":52: (Ordering Institution)"], MT202COVTag: [":52: (Ordering Institution)"]},
		        "BC": {scmPartyIndex:"R4", MT103Tag: [":59: (Beneficiary Customer)"], MT202COVTag: [":59: (Beneficiary Customer)"]},
		        "BI": {scmPartyIndex:"R5", MT103Tag: [":57: (Account With Institution)"], MT202COVTag: [":58: (Beneficiary Institution)"]},
		        "CI": {scmPartyIndex:"R6", MT103Tag: [":53: (Sender\'s Correspondent)"], MT202COVTag: ["{2 Receiver BIC}"]},
		        "II": {scmPartyIndex:"R7", MT103Tag: ["{2 Receiver BIC}"], MT202COVTag: [":56: (Intermediary)"]},
		        "AI": {scmPartyIndex:"R8", MT103Tag: [":56: (Intermediary)"], MT202COVTag: [":57: (Account With Institution)"]}
		    },
		    "7-CI": {
		        "OC": {scmPartyIndex:"R2", MT103Tag: [":50: (Ordering Customer)"], MT202COVTag: []},
		        "OI": {scmPartyIndex:"R3", MT103Tag: [":52: (Ordering Institution)"], MT202COVTag: []},
		        "BC": {scmPartyIndex:"R4", MT103Tag: [":59: (Beneficiary Customer)"], MT202COVTag: []},
		        "BI": {scmPartyIndex:"R5", MT103Tag: [":57: (Account With Institution)"], MT202COVTag: []},
		        "CI": {scmPartyIndex:"R6", MT103Tag: ["{2 Receiver BIC}"], MT202COVTag: []},
		        "II": {scmPartyIndex:"R7", MT103Tag: [":54: (Receiver\'s Correspondent)"], MT202COVTag: []},
		        "AI": {scmPartyIndex:"R8", MT103Tag: [":56: (Intermediary)"], MT202COVTag: []}
		    }
		};
		
		/**
		 * add SWIFT field tip to each party
		 * parameter configObj = {numOfParty:7, receiverOfMT103: "BI", pdIndex:1, fieldPrefix: "SwiftFieldId"}
		 */
		function addSwiftFieldTip(configObj){
			var mappedObj = SCMToSWIFTMapping[configObj.numOfParty+"-"+configObj.receiverOfMT103];
		    for(var property in mappedObj){
		        var partyMappingObj = mappedObj[property];
		        var scmPartyIndex = partyMappingObj.scmPartyIndex;
		        var MT103TagText = "";
		        if(partyMappingObj.MT103Tag && partyMappingObj.MT103Tag.length > 0) {
		            MT103TagText = "MT103 ";
		            for(var i=0; i<partyMappingObj.MT103Tag.length; i++){
		                if(i>0) MT103TagText += ";";
		                MT103TagText += partyMappingObj.MT103Tag[i];
		            }
		        }
		        var MT202COVTagText = "";
		        if(partyMappingObj.MT202COVTag && partyMappingObj.MT202COVTag.length > 0) {
		            MT202COVTagText = "MT202COV ";
		            for(var i=0; i<partyMappingObj.MT202COVTag.length; i++){
		                if(i>0) MT202COVTagText += ";";
		                MT202COVTagText += partyMappingObj.MT202COVTag[i];
		            }
		        }
		        //0R8SwiftFieldId
		        var value = (MT103TagText || "");
		        if(value != "" && MT202COVTagText != "") value += ("     " + MT202COVTagText);
		        else if(value == "" && MT202COVTagText != "") value = MT202COVTagText;
		        // SwiftFieldId1R2
		        return {"mapToSwift" : value,
		        	"scmPartyIndex" : scmPartyIndex};
//		        JSAF.getElementById(configObj.fieldPrefix+configObj.pdIndex+scmPartyIndex).innerHTML = value;
		    }	
		}
		
		return {
			addSwiftFieldTip : addSwiftFieldTip
		};
	}
})(angular);
